package org.ellisande.grid.tags;

import java.util.EnumSet;

import org.ellisande.grid.tags.support.TagBuilder;
import org.ellisande.tld.annotations.Attribute;
import org.ellisande.tld.annotations.Description;
import org.ellisande.tld.annotations.Required;

public abstract class InputTag extends AbstractTag {

	private static final long serialVersionUID = 9042829617718019611L;

	protected static enum Type {
		CHECKBOX("checkbox"), URL("url"), TEXT("text"), PASSWORD("password"), FILE(
				"file"), EMAIL("email");

		private String type;

		private Type(String type) {
			this.type = type;
		}

		public String type() {
			return type;
		}
	}

	public static enum Size {
		MEDIUM("input-medium"), LARGE("input-large"), XLARGE("input-xlarge"), SMALL(
				"input-small"), MINI("input-mini");

		private String size;

		private Size(String size) {
			this.size = size;
		}

		public String style() {
			return size;
		}

		public boolean equals(Size size) {
			return name().equalsIgnoreCase(size.name());
		}

		public boolean equals(String size) {
			return name().equalsIgnoreCase(size);
		}
	}

	@Attribute
	@Required
	@Description("Unique name of this input element. This name will be used to associate labels and reference the input value later.")
	protected String name = null;

	@Attribute
	@Description("Text that will be displayed inside the input.")
	protected String value = "";

	@Attribute
	@Description("Determines whether or not this field is required for submission. Allowed values: true | false. Default value: false.")
	protected boolean required = false;

	@Attribute
	@Description("Determines whether or not the input should be disabled. Allowed values: true | false. Default value: false.")
	protected boolean disabled = false;

	@Attribute
	@Description("Defines the size of this input field. The size can either be a predefined value (medium, large, etc) or a number which defines the number of grid columns that it should span. Allowed values: medium | large | xlarge | small | mini | 1-12. Default value: medium.")
	protected Size size = Size.MEDIUM;

	public void setName(String name) {
		this.name = name;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	public void setSize(String newSize) {
		for (Size size : EnumSet.allOf(Size.class)) {
			if (size.equals(newSize)) {
				this.size = size;
				return;
			}
		}

		this.size = Size.MEDIUM;
	}

	@Override
	public void release() {
		super.release();
		name = null;
		value = null;
		size = null;
		required = false;
		disabled = false;
	}

	public TagBuilder createInputTagBuilder() {
		final TagBuilder input = new TagBuilder("input");
		input.addStyle(size.style());
		input.addAttribute("name", name);
		if (value != null && !value.isEmpty()) {
			input.addAttribute("value", value);
		}
		if (required) {
			input.addAttribute("required", "true");
		}
		if (disabled) {
			input.addStyle("disabled");
		}

		return input;
	}

	public String getName() {
		return name;
	}

	public String getValue() {
		return value;
	}

	public boolean isRequired() {
		return required;
	}

	public boolean isDisabled() {
		return disabled;
	}

	public Size getSize() {
		return size;
	}

	public void setSize(Size size) {
		this.size = size;
	}

}
